﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Monti.Docking
{
   /// <summary>
   /// The template selector used for the docking manager.
   /// </summary>
   public class CustomTemplateSelector : DataTemplateSelector
   {
      /// <summary>
      /// Initializes a new custom template selector object.
      /// </summary>
      public CustomTemplateSelector ()
      {
         this.DataTemplates = new Dictionary <Type, DataTemplate> ();
      }

      /// <summary>
      /// Gets the dictionary with the available data templates.
      /// </summary>
      public Dictionary <Type, DataTemplate> DataTemplates { get; private set; }

      /// <summary>
      /// When overridden in a derived class, returns a
      /// <see cref="T:System.Windows.DataTemplate"/> based on custom logic.
      /// </summary>
      /// <param name="item">The data object for which to select the template.</param>
      /// <param name="container">The data-bound object.</param>
      /// <returns>Returns a <see cref="T:System.Windows.DataTemplate"/> or null. The default value is null.</returns>
      public override DataTemplate SelectTemplate (object item, DependencyObject container)
      {
         if (item != null)
         {
            var itemType = item.GetType ();
            foreach (var dataTemplate in DataTemplates)
            {
               if (itemType == dataTemplate.Key)
                  return dataTemplate.Value;
            }
         }

         return base.SelectTemplate (item, container);
      }
   }
}
